//@ sourceURL=/static/modules/employee_manage/employee_add.js

var employeeAddView = new View({
	name: "employee_add"
});

employeeAddView.type = '';
if (path[employeeAddView.name]) {
	if (path[employeeAddView.name].indexOf('user/edit') > -1) {
		//edit
		employeeAddView.type = "edit";
		$('.d-password').remove();
		$('.d-password2').remove();
		$('input[name=userName]').attr('readonly', 'readonly');
		//设置角色选中
		if (userVO.roleIds) {
			userVO.roleIds.forEach(function (id) {
				$('#role-' + id).iCheck('check');
			})
		}

		//状态
		$('[name=status][value=0]').attr('checked', false);
		$('[name=status][value=1]').attr('checked', false);
		$('[name=status][value=' + userVO.status + ']').click();

		$('#employee_add_container .page-title').html('编辑员工');
	} else {
		//add
		employeeAddView.type = "add";
		$('#employee_add_container .page-title').html('新建员工');
	}
}

employeeAddView.use(new Form({
	element: "#employee_add_form",
	type: 'add',
	url: employeeAddView.type == 'edit' ? 'user/editSave' : "/user/addSave",
	before_callback: function (data) {
		if (!data.roleIds || data.roleIds.length == 0) {
			alertify.error('请选择角色');
			data = false;
		}
		return data;
	},
	success: function (data) {
		if (employeeAddView.type == 'edit') {
			alertify.success('员工修改成功!');
		} else {
			alertify.success('员工添加成功!');
		}
		pjaxInit('/workshop?key=employee_manage');
	},
	error: function () {
		if (employeeAddView.type == 'edit') {
			alertify.error('员工修改失败!');
		} else {
			alertify.error('员工添加失败!');
		}

	}
}));

/*
部门管理tree  选择部门
 */
employeeAddView.use(new Tree({
	name: "selected_employee_tree",
	container: "#selected_employee_container",
	data: departmentTree,
	check_callback: true,
	delay: true,
	select_callback: function (data) {
		//点击节点
		if (data.node.parent && data.node.parent != '#') {
			var node = data.node;
			var nodeId = node.id;
			var nodeName = node.text;
			var parentId = node.parent;
			employeeAddView.plugins['selected_employee_dialog'].close();
			$('input[name=departmentName]').val(nodeName);
			$('input[name=deptId]').val(nodeId);
			$('input[name=departmentName]').trigger('input');
		}

	},
	change_callback: function (data) {
		u.log(2);
	},
	load_callback: function (e, data) {
		u.log(3);
	}
})); 